home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Folder Watching / Folder Watcher FBA ƒ / Sources / FBAAppleEvents.c < prev    next >
Encoding:
Text File  |  1996-02-16  |  5.8 KB  |  237 lines  |  [TEXT/CWIE]

  1. // FBAAppleEvents.c
  2. //
  3. // Folder Watcher FBA by Greg Sutton
  4. // ©Apple Computer Inc 1996, all rights reserved.
  5.  
  6.  
  7. #include "FBAAppleEvents.h"
  8.  
  9. #include "FBA.h"
  10. #include "FBALists.h"
  11.  
  12. #include <Events.h>
  13. #include <Errors.h>
  14. #include <AERegistry.h>
  15. #include <GestaltEqu.h>
  16. #include <Resources.h>
  17.  
  18. // prototypes
  19. static pascal OSErr HandleOpenApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  20. static pascal OSErr HandleQuitApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  21. static pascal OSErr HandleOpenDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  22. static pascal OSErr HandlePrintDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  23.  
  24. static OSErr        HandleOpenDesc( AEDesc* theDesc );
  25. static OSErr        GetDescriptorData( const AEDesc* theDesc, Ptr destPtr, Size maxSize );
  26.  
  27. static OSErr        GetTargetAppDesc( OSType theAppCreator, AEDesc* theTarget );
  28.  
  29.  
  30. // Globals
  31. extern    short        gQuit;
  32.  
  33.  
  34. OSErr InitAppleEvents ( void )
  35. {
  36.     short err;
  37.     long response;
  38.     
  39.     err = Gestalt( gestaltAppleEventsAttr, &response );
  40.     if (noErr != err)
  41.         response = 0L;
  42.  
  43.     if ( ! ( response & (1 << gestaltAppleEventsPresent)) )
  44.         return gestaltUndefSelectorErr;
  45.     
  46.     err = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication,
  47.                         NewAEEventHandlerProc(HandleOpenApplication), 0L, false );
  48.     if (noErr != err) goto done;
  49.  
  50.     err = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication,
  51.                         NewAEEventHandlerProc(HandleQuitApplication), 0L, false );
  52.     if (noErr != err) goto done;
  53.  
  54.     err = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments,
  55.                         NewAEEventHandlerProc(HandleOpenDocuments), 0L, false );
  56.     if (noErr != err) goto done;
  57.  
  58.     err = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments,
  59.                         NewAEEventHandlerProc(HandlePrintDocuments), 0L, false );
  60.  
  61. done:        
  62.     return err;
  63. }
  64.  
  65.  
  66.  
  67. OSErr DoAppleEvent( EventRecord *event )
  68. {
  69.     short err = noErr;
  70.     
  71.     err = AEProcessAppleEvent ( event );
  72.     return err;
  73. }
  74.  
  75.  
  76.  
  77. static pascal OSErr HandleOpenApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  78. {
  79. #ifdef __MWERKS__
  80.     #pragma unused (theAppleEvent, theReply, theRefcon )
  81. #endif
  82.     // don't do anything here
  83.     return noErr;
  84. }
  85.  
  86.  
  87.  
  88. static pascal OSErr HandleQuitApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  89. {
  90. #ifdef __MWERKS__
  91.     #pragma unused (theAppleEvent, theReply, theRefcon )
  92. #endif
  93.  
  94.     gQuit = true;
  95.     return noErr;
  96. }
  97.  
  98.  
  99. // If we get a folder dropped onto the application then we'll add it to our
  100. // list of folders to watch.
  101.  
  102. static pascal OSErr HandleOpenDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  103. {
  104. #ifdef __MWERKS__
  105.     #pragma unused( theReply, theRefcon )
  106. #endif
  107.  
  108.     AEDesc    directObj = { typeNull, NULL };
  109.     OSErr    err;
  110.  
  111.     err = AEGetParamDesc( theAppleEvent, keyDirectObject, typeWildCard, &directObj );
  112.     if ( noErr != err ) goto done;
  113.  
  114.     err = HandleOpenDesc( &directObj );
  115.  
  116. done:
  117.     AEDisposeDesc( &directObj );
  118.  
  119.     return err;
  120. }
  121.  
  122.  
  123. static OSErr    HandleOpenDesc( AEDesc* theDesc )
  124. {
  125.     AEDesc        aDesc = { typeNull, NULL };
  126.     FSSpec        anFSSpec;
  127.     long        aCount;
  128.     DescType    anAEKeyword;
  129.     OSErr        err;
  130.  
  131.     switch ( theDesc->descriptorType )
  132.     {
  133.         case typeAEList:
  134.             err = AECountItems( theDesc, &aCount );
  135.             if ( noErr != err ) goto done;
  136.             
  137.             for ( ; aCount > 0; aCount-- )
  138.             {
  139.                 err = AEGetNthDesc( theDesc, aCount, typeWildCard, &anAEKeyword, &aDesc );
  140.                 if ( noErr != err ) goto done;
  141.                 
  142.                 err = HandleOpenDesc( &aDesc );
  143.                 if ( noErr != err ) goto done;
  144.                 
  145.                 AEDisposeDesc( &aDesc );
  146.             }
  147.             break;
  148.         
  149.         default:
  150.             err = AECoerceDesc( theDesc, typeFSS, &aDesc );
  151.             if ( noErr != err ) goto done;
  152.             
  153.             err = GetDescriptorData( &aDesc, (Ptr)&anFSSpec, sizeof( anFSSpec ) );
  154.             if ( noErr != err ) goto done;
  155.             
  156.             if ( ! FolderInList( &anFSSpec ) )
  157.                 if ( AddFolder( &anFSSpec ) )            // If this is a new folder to watch
  158.                     AddFolderPathResource( &anFSSpec );    // then add it to our resources.
  159.     }
  160.  
  161. done:
  162.     AEDisposeDesc( &aDesc );
  163.     
  164.     return err;
  165. }
  166.  
  167.  
  168. static OSErr    GetDescriptorData( const AEDesc* theDesc, Ptr destPtr, Size maxSize )
  169. {
  170.     Size    copySize;
  171.     OSErr    err;
  172.     
  173.     if ( typeNull == theDesc->descriptorType || ! theDesc->dataHandle )
  174.         return( errAENotAEDesc );
  175.  
  176.     copySize = GetHandleSize( (Handle)theDesc->dataHandle );
  177.     if ( copySize <= maxSize )
  178.     {
  179.         HLock( (Handle)theDesc->dataHandle );
  180.         BlockMoveData( *theDesc->dataHandle, destPtr, copySize );
  181.         HUnlock( (Handle)theDesc->dataHandle );
  182.     }
  183.     else
  184.         err = errAECorruptData;
  185.     
  186.  
  187.     return noErr;
  188. } // GetDescriptorData
  189.  
  190.  
  191.  
  192. static pascal OSErr HandlePrintDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  193. {
  194. #ifdef __MWERKS__
  195.     #pragma unused(theAppleEvent, theReply, theRefcon )
  196. #endif
  197.  
  198.     // Faceless Background Applications can't print documents
  199.     return errAEEventNotHandled;
  200. }
  201.  
  202.  
  203. OSErr    SendChangeEvent( ProcessSerialNumber* thePSN, FSSpec* theSpec, DescType theChangeType )
  204. {
  205.     AppleEvent         anAppleEvent = {typeNull,NULL},
  206.                     aReply = {typeNull,NULL};
  207.     AEDesc           targetDesc = {typeNull,NULL},
  208.                     specDesc = {typeNull,NULL};
  209.     OSErr           err;
  210.     
  211.     err = AECreateDesc( typeProcessSerialNumber, (Ptr)thePSN, 
  212.                                      sizeof( ProcessSerialNumber ), &targetDesc );
  213.     if ( noErr != err ) goto done;
  214.  
  215.         // Create the AppleEvent
  216.     err = AECreateAppleEvent( kFolderWatcherSuite, theChangeType, &targetDesc,
  217.                                 kAutoGenerateReturnID, kAnyTransactionID, &anAppleEvent );
  218.     if ( noErr != err ) goto done;
  219.     
  220.     err = AECreateDesc( typeFSS, (Ptr)theSpec, sizeof( FSSpec ), &specDesc );
  221.     if ( noErr != err ) goto done;
  222.    
  223.     err = AEPutParamDesc(&anAppleEvent, keyDirectObject, &specDesc );
  224.     if ( noErr != err ) goto done;
  225.          
  226.     err = AESend( &anAppleEvent, &aReply, kAENoReply, kAENormalPriority,
  227.                                                 kAEDefaultTimeout, NULL, NULL );
  228.  
  229. done:
  230.     AEDisposeDesc( &anAppleEvent );
  231.     AEDisposeDesc( &aReply );
  232.     AEDisposeDesc( &targetDesc );
  233.     AEDisposeDesc( &specDesc );
  234.         
  235.     return err;
  236. }
  237.